package com.zzs.server.service.impl;

import com.zzs.common.core.enums.ResponseCode;
import com.zzs.common.core.util.SnowflakeUtil;
import com.zzs.common.security.exception.ServiceException;
import com.zzs.server.dao.mysql.ProblemCodeMapper;
import com.zzs.common.core.entity.mysql.ProblemCodeDO;
import com.zzs.server.pojo.bo.code.SaveProblemCodeBO;
import com.zzs.server.pojo.bo.code.UpdateProblemCodeBO;
import com.zzs.server.pojo.vo.ProblemCodeDetailVO;
import com.zzs.server.service.ProblemCodeService;
import com.zzs.server.service.ProblemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author zzs
 * @date 2025/03/18
 */
@Service
public class ProblemCodeServiceImpl implements ProblemCodeService {
    @Autowired
    private ProblemCodeMapper problemCodeMapper;
    @Autowired
    private ProblemService problemService;

    @Override
    public void saveProblemCode(SaveProblemCodeBO saveProblemCodeBO) {
        problemService.checkExistsAndPublish(saveProblemCodeBO.getProblemId());
        boolean existsProblemCode = problemCodeMapper.exists(saveProblemCodeBO.getProblemId(), saveProblemCodeBO.getLanguage());
        if (existsProblemCode) {
            throw new ServiceException(ResponseCode.RESOURCE_CONFLICT, "该题目代码类型已存在");
        }
        ProblemCodeDO problemCodeDO = new ProblemCodeDO();
        BeanUtils.copyProperties(saveProblemCodeBO, problemCodeDO);
        problemCodeDO.setId(SnowflakeUtil.nextId());
        int effectRows = problemCodeMapper.save(problemCodeDO);
        if (effectRows != 1) {
            throw new ServiceException(ResponseCode.DATABASE_ERROR);
        }
    }

    @Override
    public void updateProblemCode(UpdateProblemCodeBO updateProblemCodeBO) {
        problemService.checkExistsAndPublish(updateProblemCodeBO.getProblemId());
        ProblemCodeDO problemCodeDO = new ProblemCodeDO();
        BeanUtils.copyProperties(updateProblemCodeBO, problemCodeDO);
        int effectRows = problemCodeMapper.update(problemCodeDO);
        if (effectRows != 1) {
            throw new ServiceException(ResponseCode.NOT_FOUND);
        }
    }

    @Override
    public ProblemCodeDetailVO getProblemCode(long problemId, int language) {
        ProblemCodeDO problemCodeDO = problemCodeMapper.get(problemId, language);
        if (problemCodeDO == null) {
            throw new ServiceException(ResponseCode.NOT_FOUND);
        }
        ProblemCodeDetailVO problemCodeDetailVO = new ProblemCodeDetailVO();
        BeanUtils.copyProperties(problemCodeDO, problemCodeDetailVO);
        return problemCodeDetailVO;
    }


    @Override
    public void removeProblemCode(long problemId, int language) {
        problemService.checkExistsAndPublish(problemId);
        int effectRows = problemCodeMapper.remove(problemId, language);
        if (effectRows != 1) {
            throw new ServiceException(ResponseCode.NOT_FOUND, "找不到对应的题目代码");
        }
    }
}
